package com.example.home.loveDonation.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.loveDonation.adapter.CaringwelfareAdapter;
import com.example.home.loveDonation.adapter.CaringwelfarelistApter;
import com.example.home.loveDonation.adapter.WelfareclassificationApter;
import com.example.home.loveDonation.bean.BenefitListBean;
import com.example.home.loveDonation.bean.CaringwelfareBean;
import com.example.home.loveDonation.bean.DonateListBean;
import com.example.home.tool.OKHttpTool;

import java.util.Timer;
import java.util.TimerTask;

public class CaringwelfareActivity extends AppCompatActivity {

    private ViewPager vp_banner;
    private int currentPage = 0;
    private Timer timer;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;
    private CaringwelfareAdapter adapter;
    private RecyclerView benefit_list;
    private RecyclerView donate_list;
    private RecyclerView recommend_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caringwelfare);

        initView();

        //轮播图
        getSlideshow();
        startAutoScroll();

        //公益分类
        getBenefitList();

        //捐赠次数和实时捐赠列表
        getDonateList();
    }

    private void getDonateList() {
        OKHttpTool.doNetRequest("prod-api/api/public-welfare/public-welfare-activity/recommend-list",
                OKHttpTool.GET,
                DonateListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        DonateListBean donateListBean = (DonateListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindDonateList(donateListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindDonateList(DonateListBean donateListBean) {
        RecyclerView recyclerView = findViewById(R.id.donate_list);
        CaringwelfarelistApter caringwelfarelistApter = new CaringwelfarelistApter(CaringwelfareActivity.this, donateListBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CaringwelfareActivity.this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(caringwelfarelistApter);
    }

    private void getBenefitList() {
        OKHttpTool.doNetRequest("prod-api/api/public-welfare/public-welfare-type/list",
                OKHttpTool.GET,
                BenefitListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        BenefitListBean benefitListBean = (BenefitListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBenefitList(benefitListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindBenefitList(BenefitListBean benefitListBean) {
        RecyclerView recyclerView = findViewById(R.id.benefit_list);
        WelfareclassificationApter welfareclassificationApter = new WelfareclassificationApter(CaringwelfareActivity.this, benefitListBean.getData());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(CaringwelfareActivity.this, 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(welfareclassificationApter);
    }

    private void getSlideshow() {
        OKHttpTool.doNetRequest("prod-api/api/public-welfare/ad-banner/list",
                OKHttpTool.GET,
                CaringwelfareBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        CaringwelfareBean caringwelfareBean = (CaringwelfareBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindSlideshow(caringwelfareBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindSlideshow(CaringwelfareBean caringwelfareBean) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        adapter = new CaringwelfareAdapter(this, caringwelfareBean.getData());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == adapter.getCount()) {
                    currentPage = 0;
                }
                vp_banner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    private void initView() {
        vp_banner = findViewById(R.id.vp_banner);
        ImageView back = findViewById(R.id.back);
        EditText search_et = findViewById(R.id.search_et);
        TextView search_btn = findViewById(R.id.search_btn);
        benefit_list = findViewById(R.id.benefit_list);
        donate_list = findViewById(R.id.donate_list);
        recommend_list = findViewById(R.id.recommend_list);


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CaringwelfareActivity.this.finish();
            }
        });
    }
}